/**
 * 
 */
package org.openwork.portlet.skillmanagement.service.persistence;

import java.util.ArrayList;
import java.util.List;

import org.openwork.portlet.skillmanagement.model.SkillUser;
import org.openwork.portlet.skillmanagement.model.impl.SkillUserModelImpl;

import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

/**
 * @author samuelgmartinez
 *
 */
public class SkillUserFinderImpl extends BasePersistenceImpl<SkillUser> implements SkillUserFinder {

	public static final String _SQL_SELECT_SKILLUSER_WHERE = "SELECT skillUser FROM SkillUser skillUser WHERE ";
	public static final String _SQL_COUNT_SKILLUSER_WHERE = "SELECT COUNT(skillUser) FROM SkillUser skillUser WHERE ";
	public static final String _FINDER_COLUMN_SKILLID_SKILLID = "skillUser.id.skillId = ?";
	public static final String _ORDER_BY_ENTITY_ALIAS = "skillUser.";  	 
	
	@BeanReference(type = SkillUserPersistence.class)
	public SkillUserPersistence skillUserPersistence;
	
	public static final FinderPath FINDER_PATH_COUNT_BY_SKILLID = new FinderPath(SkillUserModelImpl.ENTITY_CACHE_ENABLED,
            SkillUserModelImpl.FINDER_CACHE_ENABLED, SkillUserPersistenceImpl.FINDER_CLASS_NAME_LIST,
            "searchCountBySkillId",
            new String[] {
                Long.class.getName()
            });
	
	public static final FinderPath FINDER_PATH_FIND_BY_SKILLID = new FinderPath(SkillUserModelImpl.ENTITY_CACHE_ENABLED,
            SkillUserModelImpl.FINDER_CACHE_ENABLED, SkillUserPersistenceImpl.FINDER_CLASS_NAME_LIST,
            "searchBySkillId",
            new String[] {
                Long.class.getName(),
            "java.lang.Integer", "java.lang.Integer",
                "com.liferay.portal.kernel.util.OrderByComparator"
            });
	
	/* (non-Javadoc)
	 * @see org.openwork.portlet.skillmanagement.service.persistence.SkillUserFinder#searchBySkillId(long, int, org.openwork.portlet.skillmanagement.model.SkillUserValueComparator, int, int, com.liferay.portal.kernel.util.OrderByComparator)
	 */
	public List<SkillUser> searchBySkillId(long skillId, int start, int end,
			OrderByComparator orderByComparator) throws PortalException,
			SystemException {
		Object[] finderArgs = new Object[] {new Long(skillId), new Integer(start), new Integer(end), orderByComparator};
		List<SkillUser> result = (List<SkillUser>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_SKILLID, finderArgs, this);
		
		if(result == null) {
			Session session = null;
			
			try {
				StringBundler queryBundler = new StringBundler(_SQL_SELECT_SKILLUSER_WHERE);
				queryBundler.append(_FINDER_COLUMN_SKILLID_SKILLID);
				
				if (orderByComparator != null) {
	                appendOrderByComparator(queryBundler, _ORDER_BY_ENTITY_ALIAS,
	                    orderByComparator);
	            }
				
				session = openSession();
				Query query = session.createQuery(queryBundler.toString());
				result = query.list();
			} catch(Exception ex) {
				processException(ex);
			} finally {
				if(result == null) {
					result = new ArrayList<SkillUser>();
				}
				
				skillUserPersistence.cacheResult(result);
				FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_SKILLID, finderArgs, result);
				closeSession(session);
			}
			
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see org.openwork.portlet.skillmanagement.service.persistence.SkillUserFinder#searchCountBySkillId(long, int, org.openwork.portlet.skillmanagement.model.SkillUserValueComparator)
	 */
	public int searchCountBySkillId(long skillId) throws PortalException,
			SystemException {
		Object[] finderArgs = new Object[] {new Long(skillId)};
		Long result = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_SKILLID, finderArgs, this);
		
		if(result == null) {
			Session session = null;
			
			try {
				StringBundler queryBundler = new StringBundler(_SQL_COUNT_SKILLUSER_WHERE);
				queryBundler.append(_FINDER_COLUMN_SKILLID_SKILLID);
								
				session = openSession();
				Query query = session.createQuery(queryBundler.toString());
				result = (Long)query.uniqueResult();
			} catch(Exception ex) {
				processException(ex);
			} finally {
				if(result == null) {
					result = 0l;
				}
				
				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_SKILLID, finderArgs, result);
				closeSession(session);
			}
		}
		
		return result.intValue();
	}

	public SkillUserPersistence getSkillUserPersistence() {
		return skillUserPersistence;
	}

	public void setSkillUserPersistence(SkillUserPersistence skillUserPersistence) {
		this.skillUserPersistence = skillUserPersistence;
	}

}
